<script type="text/javascript" >
    jQuery("#adminHeader a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
    stSocialMediaToolbox.sortColumn();
    stSocialMediaToolbox.shortAction();
    stSocialMediaToolboxCustom.switchButton();
</script>
<div class="buttonActionContainer">
    <a href="javascript:iconNewFunction()" class="button add-new-h2 buttonAction">
        <?php _e($iconNew, $config['plugin_name'])?>
    </a>
    <a href="javascript:stSocialMediaToolbox.loadPage('userEdit')" class="button add-new-h2 buttonAction">
        <?php _e('Create new user', $config['plugin_name'])?>
    </a>
</div>
<div class="w100p bgl">
    <div class="w100p bgr">
        <div class="inner_container">
            <div class="filter">
                <label><?php _e('Company name', $config['plugin_name']) ?>:</label>
                <select id="filter_company_name"
                        onchange="
                            var data_post = new Array();
                            data_post['filter_company_name'] = jQuery(this).val();
                            data_post['filter_user_level'] = jQuery('#filter_user_level').val();
                            stSocialMediaToolbox.loadPage('<?php echo $current_action?>', data_post, false);
                        "
                >
                    <option <?php echo $filter_company_name == 'all' ? 'selected' : '' ?> value="all">Select user's company name</option>
                    <?php
                    foreach ($company_name as $value) {
                        if ($filter_company_name == $value) {
                            $select = 'selected';
                        } else {
                            $select = '';
                        }
                    ?>
                        <option <?php echo $select ?> value="<?php echo $value ?>"><?php echo $value ?></option>
                    <?php
                    }
                    ?>
                </select>
                <label><?php _e('User level', $config['plugin_name']) ?>:</label>
                <select id="filter_user_level" onchange="
                            var data_post = new Array();
                            data_post['filter_company_name'] = jQuery('#filter_company_name').val();
                            data_post['filter_user_level'] = jQuery(this).val();
                            stSocialMediaToolbox.loadPage('<?php echo $current_action?>', data_post, false);
                        "
                >
                    <option <?php echo $filter_user_level == 'all' ? 'selected' : '' ?> value="all">Select user's role</option>
                    <option <?php echo $filter_user_level == 'editor'? 'selected':''?> value="editor">Client's administrator</option>
                    <option <?php echo $filter_user_level == 'author'? 'selected':''?> value="author">Client's user</option>
                    <option <?php echo $filter_user_level == 'administrator'? 'selected':''?> value="administrator">Master administrator</option>
                </select>
                <br clear="both" />
                
            </div>
            <br clear="both" /><br clear="both" />
            <table class="widefat" cellspacing="0">
                <thead>
                    <tr>
                        <?php print_column_headers('stSocialMediaToolbox_st_pluginperm'); ?>
                    </tr>
                </thead>

                <tbody>
                <?php
                if (count($results) > 0) {
                    $tr_class = "";
                    foreach ($results as $result) {
                        if( empty($tr_class) || $tr_class == "class='bg_gray2'"){
                            $tr_class = "class='bg_gray1'";
                        }else{
                            $tr_class = "class='bg_gray2'";
                        }
                        $plugin_name = explode(',', $result->plugin_name);
                        $user_login = get_user_by('id', $result->user_id);                        
                        $user_login = $user_login->user_login;
                        $user_data = get_user_by('id', $result->user_id);
                        if ($user_data && is_array($user_data->wp_capabilities)) {
                            foreach ($user_data->wp_capabilities as $key => $value) {
                                $role = $key;
                            }
                        }
                        switch ($role) {
                            case 'administrator':
                                $role = 'Master admin';
                                break;
                            case 'editor':
                                $role = 'Client admin';
                                break;
                            default:
                                $role = 'Regular user';
                                break;
                        }
//                        $user_parent = get_user_by('id', $result->parent_id);
//                        $user_parent = $user_parent->user_login;
                        if($result->parent_id){
                            $user_meta = get_user_meta($result->parent_id, 'company_info', true);
                        }else{
                            $user_meta = get_user_meta($result->user_id, 'company_info', true);
                        }
                        if($user_login != $config['userAdminName']){
                            echo "<tr id='$result->id' $tr_class >"
                            . "<td>" . $user_login
                            . "<div class=\"row-actions short_action\">"
                            . "<span action='userEdit' short_action='' id='$result->id' >Edit | </span>"
                            . "<span class='trash' short_action='delete' id='$result->user_id' confirm='Are you sure you want to remove this user?'>Remove </span>"
                            . "</div>"
                            . "</td>"
                            . "<td>" . $user_meta['company_name'] . "</td>"
                            . "<td>" . $role . "</td>"
                            ;
                        
                            if(count($addons)>0){
                                foreach ($addons as $key => $value) {
                                    if(!empty($key)){
                                        if(in_array($key, $plugin_name)){
                                            $status = 'cb-enable';
                                        }else{
                                            $status = 'cb-disable';
                                        }
                                        echo "<td>"
                                        . '<div userID="'.$result->user_id.'" name="'.$key.'" class="' . $status . ' switch_checkbox">'
                                        . '</div>'
                                        . "</td>"
                                        ;
                                    }

                                }
                            }else{
                                echo "<td>"
                                    . 'none'
                                    . "</td>"
                                    ;
                            }
                            echo "</tr>"
                            ;
                        }
                    }
                }
                ?>
                </tbody>
            </table>
            <div class="page_d">
                <?php echo $pagination?>
                <div class="cl"></div>
            </div>

        </div>
    </div>
</div>